/*
 * ShoppingCartInvalidClearListItem.java
 * business
 *
 * Created by ChenTao on 2016/9/13.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.content.Context;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.model.IShoppingCartModel;
import com.yidiandao.business.data.model.ShoppingCartModel;
import com.yidiandao.business.manager.ShoppingCartManager;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.ShoppingCartInvalidClearHolder;
import com.yidiandao.business.utils.UIUtils;

/** 清除失效商品 */
public class ShoppingCartInvalidClearListItem
        extends DisplayListItem<ShoppingCartInvalidClearHolder, Object> {

    private ShoppingCartModel mShoppingCartModel;

    public ShoppingCartInvalidClearListItem(Object o) {
        super(o);
        mShoppingCartModel = new ShoppingCartModel();
    }

    @Override
    public void onShow(final Context context, ShoppingCartInvalidClearHolder holder) {
        holder.mTvClear.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 清除失效商品
                        // 从购物车清除、从服务器清除

                        final LoadingDialog loadingDialog = new LoadingDialog(context);
                        loadingDialog.setCanceledOnTouchOutside(false);
                        loadingDialog.show();

                        mShoppingCartModel.deleteGoodsFromServer(
                                ShoppingCartManager.getInstance().getInvalidGoodsRecIds(),
                                getDisplayItemTag(),
                                new IShoppingCartModel.OnShoppingCartOperatorListener() {
                                    @Override
                                    public void onSuccess() {
                                        loadingDialog.dismiss();

                                        UIUtils.showToast(
                                                UIUtils.getString(R.string.clear_success));
                                        // 删除成功后从本地删除失效商品、并刷新购物车
                                        ShoppingCartManager.getInstance().clearInvalidGoodsList();
                                        // 再刷新购物车列表,需要重新刷新购物车列表
                                        mShoppingCartModel.postRefreshShoppingCartState(true);
                                    }

                                    @Override
                                    public void onFailed() {
                                        loadingDialog.dismiss();
                                    }
                                });
                    }
                });
    }

    @Override
    public String getDisplayItemTag() {
        return ShoppingCartInvalidClearListItem.class.getSimpleName();
    }

    @Override
    public Class<ShoppingCartInvalidClearHolder> getViewHolderClazz() {
        return ShoppingCartInvalidClearHolder.class;
    }
}
